package libs

import (
	"github.com/patrickmn/go-cache"
	"sync"
	"time"
)

var CacheObj *cache.Cache

//初始化缓存配置 只能初始化一次
func InitCache() {
	var initOnce sync.Once
	initOnce.Do(func() {
		CacheObj = cache.New(30*time.Second, 10*time.Second)
	})
}

//设置缓存
func SetCacheDefault(cacheKey string, val interface{}, d time.Duration) {
	if d > 0 {
		CacheObj.Set(cacheKey, val, d)
	}
}

//获取缓存
func GetCache(key string) (value interface{}, found bool) {
	value, found = CacheObj.Get(key)
	return
}
